import { Component, OnInit } from '@angular/core';
import { isEmpty } from 'rxjs/operators';
import { Utils } from 'src/app/providers/Utils';
import { ToastService } from 'src/app/providers/toast/toast.service';
import { Storage } from 'src/app/providers/Storage';
import { AuthService } from '../../../services/auth.service'
import * as constants from '../../../providers/Constants'
import { NavController } from '@ionic/angular';
@Component({
  selector: 'app-updata-password',
  templateUrl: './updata-password.page.html',
  styleUrls: ['./updata-password.page.scss'],
})
export class UpdataPasswordPage implements OnInit {
  emailStr: string = '';
  codeStr: string = '';
  oldPswStr: string = '';
  newPswStr: string = '';
  // 验证码倒计时
  verifyCode: any = {
    verifyCodeTips: "Get code",
    countdown: constants.DEFAULT_SMS_TIME,
    disable: true
  }
  constructor(private toast: ToastService, public nav: NavController, public service: AuthService) { }

  ngOnInit() {
  }

  onBtn() {
    let email = Storage.localStorage.get('userPhone');
    if (Utils.isEmpty(this.emailStr)) {
      this.toast.show('The mailbox cannot be empty');
    } else if (email != this.emailStr) {
      this.toast.show('Email error');
    } else if (Utils.isEmpty(this.codeStr)) {
      this.toast.show('The verification code cannot be empty');
    } else if (Utils.isEmpty(this.oldPswStr)) {
      this.toast.show('Please enter the old password');
    } else if (Utils.isEmpty(this.newPswStr)) {
      this.toast.show('Please enter a new password');
    } else {
      let data = {
        userEmail: this.emailStr,
        userPassword: this.oldPswStr,
        code: this.codeStr,
        email: this.codeStr,
        newPassword: this.newPswStr
      }
      this.service.changePassWord(data).subscribe(res => {
        // 保存成功返沪上一页，否则弹出提示框
        if (res.retCode == '000000') {
          this.toast.show('Please login again!');
          Storage.localStorage.clear();
          Storage.sessionStorage.clear()
          this.nav.navigateForward("login", {
            queryParams: {
              pageType: 1,
            }
          })
        } else if (constants.errCode[res.retCode]) {
          if (constants.errCode[res.retCode] != '0') { this.toast.show(constants.errCode[res.retCode]); }
        } else if (res.retMsg) {
          this.toast.show(res.retMsg);
        } else {
          this.toast.show("exception service routine");
        }
      });
    }
  }





  getSms() {
    if (!this.emailStr) {
      this.toast.show('The mailbox cannot be empty');
    } else if (!Utils.isEmail(this.emailStr)) {
      this.toast.show('Mailbox format error');
    } else {
      if (this.verifyCode.disable) {
        let data = {
          email: this.emailStr,
          type: 1,
        }
        this.service.getSms(data).subscribe(res => {
          // 保存成功返沪上一页，否则弹出提示框
          if (res.retCode == '000000') {
            this.toast.show("Send a success");
            this.settime();
          } else {
            if (constants.errCode[res.retCode]) {
              if (constants.errCode[res.retCode] != '0') { this.toast.show(constants.errCode[res.retCode]); }
            } else if (res.retMsg) {
              this.toast.show(res.retMsg);
            } else {
              this.toast.show("Mailbox sending exception");
            }
          }
        });
      }
    }
  }
  // 倒计时
  settime() {
    if (this.verifyCode.countdown == 1) {
      this.verifyCode.countdown = constants.DEFAULT_SMS_TIME;
      this.verifyCode.verifyCodeTips = "Get code";
      this.verifyCode.disable = true;
      return;
    } else {
      this.verifyCode.disable = false;
      this.verifyCode.countdown--;
    }
    this.verifyCode.verifyCodeTips = "Get (" + this.verifyCode.countdown + ")";
    setTimeout(() => {
      this.verifyCode.verifyCodeTips = "Get (" + this.verifyCode.countdown + ")";
      this.settime();
    }, 1000);
  }
}